using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.ComponentModel;

namespace Lizk.Media
{
    public class MediaUtil
    {
        public static string[] BasicVideoExtensions = new string[] { "mkv", "mp4", "avi", "mpg", "ogm", "wmv", "mov", "divx", "ifo" };
        public static string[] BasicDiscImageExtensions = new string[] { "img", "cue", "iso" };
        public static string[] BasicPictureExtensions = new string[] { "jpg", "png", "gif" };

        public enum FileType 
        {
            Unknown,
            Video,
            DiscImage,
            Picture
        }
        public static FileType GetFiletype(string filePath)
        {
            string ext = Path.GetExtension(filePath).ToLower();
            if (string.IsNullOrEmpty(ext))
                return FileType.Unknown;

            ext = ext.Substring(1);

            if (Array.IndexOf<string>(BasicVideoExtensions, ext) > -1)
                return FileType.Video;

            if (Array.IndexOf<string>(BasicDiscImageExtensions, ext) > -1)
                return FileType.DiscImage;

            if (Array.IndexOf<string>(BasicPictureExtensions, ext) > -1)
                return FileType.Picture;

            return FileType.Unknown;

        }
        /*
        public static string FindSingleMediaRoot(string volumeLabel, DriveType[] acceptedTypes)
        {
            string s = null;


            foreach (DriveInfo di in DriveInfo.GetDrives())
            {

                if (Array.IndexOf<DriveType>(acceptedTypes, di.DriveType) == -1)
                    continue;

                if (di.DriveType == DriveType.Network)
                {
                    if (!NetworkDrive.IsNetworkDriveOnline(di.RootDirectory.FullName, 1000))
                        continue;

                    if (!di.IsReady)
                    {
                        try
                        {
                            NetworkDrive.RestoreConnection(di.Name);
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }
                else if (!di.IsReady)
                    continue;
            
                if (di.VolumeLabel != volumeLabel)
                    continue;


                
                s = MediaUtil.FindMediaRoot(di.RootDirectory.FullName, 1);
                break;
            }
            return s;
        }

        public static string[] FindAllMediaRoots(params DriveType[] acceptedTypes)
        {
            List<string> mediaroots = new List<string>();

            foreach (DriveInfo di in DriveInfo.GetDrives())
            {
                if (Array.IndexOf<DriveType>(acceptedTypes, di.DriveType) == -1)
                    continue;

                if (di.DriveType == DriveType.Network)
                {
                    if (!NetworkDrive.IsNetworkDriveOnline(di.RootDirectory.FullName, 1000))
                        continue;

                    if (!di.IsReady)
                    {
                        try
                        {
                            NetworkDrive.RestoreConnection(di.Name);
                        }
                        catch
                        {
                            continue;
                        }
                    }
                } else if (!di.IsReady)
                        continue;

                string s = MediaUtil.FindMediaRoot(di.RootDirectory.FullName, 1);
                if(s != null)
                    mediaroots.Add(s);
            }
            return mediaroots.ToArray();
        }
        public static string FindMediaRoot(string path, int depth)
        {
            try
            {
                if (File.Exists(Path.Combine(path, ".mediadir")))
                    return path;

                foreach (var d in Directory.GetDirectories(path))
                {
                    if (Path.GetFileName(d).ToLower() == "media")
                        return d;
                }

                if (depth > 0)
                    foreach (string d in Directory.GetDirectories(path))
                    {
                        string g = FindMediaRoot(d, depth - 1);
                        if (g != null)
                            return g;
                    }
            }
            catch
            {
                return null;
            }
            return null;
        }
*/


        public static string FindImage(string dir, ICollection<string> acceptedExtensions)
        {
            foreach (string f in Directory.GetFiles(dir, "*", SearchOption.AllDirectories))
            {
                string ext = Path.GetExtension(f).ToLower();
                if(ext.Length < 1)
                    continue;
                foreach (string e in acceptedExtensions)
                    if (ext.Substring(1).Equals(e.ToLower()))
                        return f;
            }
            return null;
        }
        public static string[] RelatedFiles(string path)
        {
            string filename = Path.GetFileNameWithoutExtension(path);
            return Directory.GetFiles(Path.GetDirectoryName(path), filename + ".*");
        }
        public static string GetMimeType(string extension)
        {
            switch (extension)
            {
                case "mkv":
                    return "video/x-matroska";
                case "mp4":
                    return "video/mp4v-es";
                case "avi":
                    return "video/x-msvideo";
                case "ogm":
                    return "video/x-mpeg";
                case "mpeg":
                    return "video/mpeg";
                default:
                    return "application/octet-stream";
            }
        }

    }
}
